package Master;

import java.io.ObjectInputStream;
import java.io.PrintWriter;
import java.net.Socket;

import Interfaces.Task;

/**
 * 
 * 	This class tries to execute the clients' task on the appropriate machine 
 *	
 *	Receives via the constructor the socket by which we communicate with the
 *  client.
 *  In order to decide on which machine (if exists) we will run the task, we 
 *  use a server method of the Master class - doTask.
 *
 */
public class ClientServiceThread  extends Thread {
	
	private Socket socket;
	
	public ClientServiceThread(Socket socket){
		this.socket=socket;
	}
	
	@Override
    public void run(){

        try {
        	
        	ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
			
			Task task = (Task)in.readObject();
			PrintWriter out = new PrintWriter(socket.getOutputStream(),true); 
			if(Master.doTask(task)) {
				out.println("OK");
			} else {
				out.println("REJECT");
			}
			
			socket.close();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.exit(1);
		}
    }

}
